angular.module('start.common.setting.ctrl', [])

  .controller('SettingCtrl', function($scope, $$CommonService, $timeout, $rootScope, $ionicPopup, $$AppUpdateService, $$LocalStorage, $$UserDataService, $location, $ionicLoading) {
    console.log("SettingCtrl");
    // 清空缓存对话框

    // 一个查看是否有新版本
    $scope.checkAppVersion = function() {
      $$CommonService.getAppVersion("" + $rootScope.config.version, ($rootScope.config.isIOS ? 2 : 1))
        .then(function(res) {
          // 强制更新
          if (res.data.isForce == 1) {
            $rootScope.appstate.suitableVsersion = res.data;
            $$LocalStorage.setObj("appstate", $rootScope.appstate);
            $ionicPopup.show({
              title: "APP版本",
              subTitle: "需要更新到最新版" + res.data.versionId,
              // scope: scope,
              buttons: [{
                text: '取消',
                type: 'button button-light',
                onTap: function() {
                  ionic.Platform.exitApp();
                }
              }, {
                text: '确定',
                type: 'button button-calm',
                onTap: function() {
                  $$AppUpdateService.downloadapk($rootScope.appstate.suitableVsersion.appUrl, true);
                }
              }]
            });
          } else {
            // 提示用户是否更新
            $rootScope.appstate.suitableVsersion = res.data;
            $$LocalStorage.setObj("appstate", $rootScope.appstate);

            if ($rootScope.config.version == res.data.versionId) {
              $ionicLoading.show({
                template: "当前为最新版" + res.data.versionId,
                noBackdrop: true,
                duration: 1500
              });
            } else {
              if ($rootScope.config.isIOS) {
                $$AppUpdateService.downloadapk($rootScope.appstate.suitableVsersion.appUrl, false);
              } else {
                // 更新当前最新版本
                $ionicPopup.show({
                  title: "更新提示",
                  subTitle: "最新版本" + $rootScope.appstate.suitableVsersion.versionId + "，是否立即更新",
                  // scope: scope,
                  buttons: [{
                    text: '暂不',
                    type: 'button button-light',
                    onTap: function() {}
                  }, {
                    text: '更新',
                    type: 'button button-calm',
                    onTap: function(e) {
                      $$AppUpdateService.downloadapk($rootScope.appstate.suitableVsersion.appUrl, false);
                    }
                  }]
                });
              }
            }

          }
        }, function(err) {
          $ionicLoading.show({
            template: "检测失败",
            noBackdrop: true,
            duration: 1500
          });
        });
    };

    $scope.surealertClear = function() {
      $ionicPopup.show({
        title: "删除数据",
        subTitle: "确认删除用户缓存数据吗？",
        // scope: scope,
        buttons: [{
          text: '取消',
          type: 'button button-light',
          onTap: function() {}
        }, {
          text: '确定',
          type: 'button button-calm',
          onTap: function() {
            // $rootScope.userData= {};
              $location.path("");
            $ionicLoading.show({
              template: "删除成功",
              noBackdrop: true,
              duration: 1500
            });
            $timeout(function() {
              $$LocalStorage.removeAll();
              window.location.reload();
            }, 600);
          }
        }]
      });
    };

  });
